package ControlLayer;
import ModelLayer.*;
import java.util.ArrayList;

public class LoanCtr
{
    private Lending _l;
    private LendingContainer _loanCont;
    private AddressBook _addrB;
    private DVDLager _dvdK;
    private DVD _d;
    private Copy _c;
    private Person _p;

    public LoanCtr()
    {
        _l = null;
        _loanCont = LendingContainer.getInstance();
        _addrB = AddressBook.getInstance();
        _dvdK = DVDLager.getInstance();
        _d = null;
        _c = null;
        _p = null;
    }
    
    public void createLoan(long personID, String title, long id, String date, String period)
    {
        _d = _dvdK.getDvdByTitle(title);
        ArrayList<Copy> _copies = _d.getAllCopies();
        _c = _copies.get(0);
        _p = _addrB.getPerson(personID);
        _l = new Lending(id, date, period, _p, _c);
        _loanCont.addLending(_l);
    }
    
    public void removeLoan(long id)
    {
        _loanCont.removeLending(id);
    }
    
    public String listAllLoans()
    {
        ArrayList<Lending> loansL = _loanCont.listAllLendings();
        String loans = "";
        
        for (Lending l : loansL)
        {
            _p = l.getPerson();
            _c = l.getCopy();
            
            loans += "ID: " + l.getID() + "\n";
            loans += "Date: " + l.getDate() + "\n";
            loans += "Period: " + l.getPeriod() + "\n";
            loans += "Person ID: " + _p.getID() + "\n";
            loans += "Copy serial: " + _c.getSerialNr() + "\n\n";
        }
        
        return loans;
    }
}
